﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Net5ApiAndEfMySql.API.Controllers;
using Net5ApiAndEfMySql.DtoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VueAndNetCoreEfApi.IService.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;

namespace VueAndNetCoreEfApi.Controllers
{
    public class PositioninfoController : BaseApiController
    {
        private IPositioninfoService _IPositioninfoService;
        public PositioninfoController(IPositioninfoService  IPositioninfoService)
        {
            _IPositioninfoService = IPositioninfoService;
        }
        //显示方法
       [AllowAnonymous]
       // [HttpGet]
       [HttpPost]
        public ResultModel PositioninfoList(SearchModel m)//显示方法
        {
            ResultModel result = new ResultModel();
            if (m == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参错误";
                return result;
            }
            try
            {
                var data = _IPositioninfoService.PositioninfoList(m);
                data.draw = m.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
      //  [AllowAnonymous]
        //信息添加方法
       // [HttpPost]
        public ResultModel PositionInfoAdd(positioninfo info)//信息添加
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(info.po_name))
            {
                result.code = ResultCode.Error;
                result.mess = "请输入职位";
                return result;
            }
            if(string.IsNullOrWhiteSpace(info.po_other_name))
            {
                result.code = ResultCode.Error;
                result.mess = "请输入别名";
                return result;
            }
            try
            {
                var resultData = _IPositioninfoService.positioninfoAdd(info);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }

            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误";
            }
             return result;
        }

        //删除方法
        [HttpPost]
        public ResultModel PositionInfoDelete(int po_id)//删除方法
        {
            ResultModel result = new ResultModel();
            if(po_id<0)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _IPositioninfoService.positioninfoDelete(po_id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误";
            }
            return result;
        }

        //修改方法
        public ResultModel PositioninfoPut(positioninfo info)//修改信息
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(info.po_name))  
            {
                result.code = ResultCode.Error;
                result.mess = "必须输职位";
                return result;
            }
            if (string.IsNullOrWhiteSpace(info.po_other_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输别名";
                return result;
            }
            try
            {
                var resultData = _IPositioninfoService.PositioninfoDel(info);
                result.code = ResultCode.Ok;
                result.data = resultData;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="os_id"></param>
        /// <returns></returns>
        public ResultModel GetOutsourcePoID(int po_id)
        {
            ResultModel result = new ResultModel();
            if (po_id< 0)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误!";
                return result;
            }
            try
            {
                var data = _IPositioninfoService.PositioninfoBypo_id(po_id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";

            }
            catch (Exception)
            {
                result.code = ResultCode.Error;
                result.mess = "错误！";
            }
            return result;
        }



    }

}
